// wifi_prov_server_simple.c - 简化版配网服务器API实现
#include "wifi_prov_server_simple.h"
#include "wifi_provisioning_server.h"

// 全局凭据回调
static wifi_prov_credentials_callback_t g_credentials_callback = NULL;

// 凭据回调包装函数
static void credentials_callback_wrapper(const char* ssid, const char* pwd, int save)
{
    if (g_credentials_callback) {
        g_credentials_callback(ssid, pwd, save);
    }
}

// 服务器回调包装
static void server_started_callback(void)
{
    printf("[prov_server] Server started\n");
}

static void server_stopped_callback(void)
{
    printf("[prov_server] Server stopped\n");
}

static void server_finished_callback(wifi_prov_result_t result)
{
    printf("[prov_server] Server finished: %d\n", result);
}

int wifi_prov_server_start(void)
{
    // 初始化服务器配置
    wifi_prov_server_config_t config = {
        .port = 80,
        .captive_portal_ip = 0xC0A80101,
        .auto_start = 0,  // 手动启动
        .enable_captive_portal = 1
    };
    
    // 初始化服务器
    int ret = wifi_provisioning_server_init(&config);
    if (ret != 0) {
        printf("[prov_server] Init failed: %d\n", ret);
        return -1;
    }
    
    // 设置回调
    wifi_prov_server_callbacks_t callbacks = {
        .on_server_started = server_started_callback,
        .on_credentials = credentials_callback_wrapper,
        .on_finished = server_finished_callback,
        .on_server_stopped = server_stopped_callback
    };
    
    ret = wifi_provisioning_server_set_callbacks(&callbacks);
    if (ret != 0) {
        printf("[prov_server] Set callbacks failed: %d\n", ret);
        return -2;
    }
    
    // 启动服务器
    ret = wifi_provisioning_server_start_async();
    if (ret != 0) {
        printf("[prov_server] Start failed: %d\n", ret);
        return -3;
    }
    
    printf("[prov_server] Server start requested\n");
    return 0;
}

int wifi_prov_server_stop(void)
{
    int ret = wifi_provisioning_server_stop_async();
    if (ret != 0) {
        printf("[prov_server] Stop failed: %d\n", ret);
        return -1;
    }
    
    printf("[prov_server] Server stop requested\n");
    return 0;
}

int wifi_prov_server_is_running(void)
{
    wifi_prov_server_state_t state = wifi_provisioning_server_get_state();
    return (state == WIFI_PROV_SERVER_RUNNING) ? 1 : 0;
}

int wifi_prov_server_set_credentials_callback(wifi_prov_credentials_callback_t callback)
{
    g_credentials_callback = callback;
    printf("[prov_server] Credentials callback set\n");
    return 0;
}

int wifi_prov_server_notify_finished(wifi_prov_server_result_t result)
{
    wifi_prov_result_t prov_result;
    
    switch (result) {
        case WIFI_PROV_SERVER_RESULT_OK:
            prov_result = WIFI_PROV_RESULT_OK;
            break;
        case WIFI_PROV_SERVER_RESULT_FAIL:
            prov_result = WIFI_PROV_RESULT_FAIL;
            break;
        case WIFI_PROV_SERVER_RESULT_ABORTED:
            prov_result = WIFI_PROV_RESULT_ABORTED;
            break;
        default:
            prov_result = WIFI_PROV_RESULT_FAIL;
            break;
    }
    
    int ret = wifi_provisioning_server_notify_finished(prov_result);
    if (ret != 0) {
        printf("[prov_server] Notify finished failed: %d\n", ret);
        return -1;
    }
    
    printf("[prov_server] Notified finished: %d\n", result);
    return 0;
}

const char* wifi_prov_server_get_status_string(void)
{
    wifi_prov_server_state_t state = wifi_provisioning_server_get_state();
    
    switch (state) {
        case WIFI_PROV_SERVER_STOPPED:
            return "STOPPED";
        case WIFI_PROV_SERVER_STARTING:
            return "STARTING";
        case WIFI_PROV_SERVER_RUNNING:
            return "RUNNING";
        case WIFI_PROV_SERVER_STOPPING:
            return "STOPPING";
        case WIFI_PROV_SERVER_ERROR:
            return "ERROR";
        default:
            return "UNKNOWN";
    }
}
